#!/usr/bin/perl
#update rss
    #    <item>
   #         <title>First Title</title>
  #          <description>First desc</description>
 #           <media:content url="family.guy.702.pdtv.xvid-sys.flv" />
#        </item>

use File::Basename;
##TESTING REMOVE WHEN DONE
#unlink("/home/wylde/scripts/fucktelus.xml") or die $!;
$path = @ARGV[0];
$xmlpath = @ARGV[0];
$xmlfile = "/home/wylde/scripts/fucktelustv.xml";

open FILE, $xmlfile;
@lines = <FILE>;
close FILE;
for ($i=0; $i<@lines; $i++)
{	
	#search for </channel> or </rss> replace with a space
	@lines[$i] =~ s/<\/channel>|<\/rss>/ /g;
}
open FILE, ">$xmlfile";# or die $!;
print FILE @lines;
close FILE;


chomp(@flvs = `find $path -type f -name '*.flv'`);
#@flvs = =~ tr/A-Z/a-z/;
#@flvs = sort { $a <=> $b } @flvs; 

open LOG, ">$xmlpath/list.xml" or die $!;
print LOG ("
        <!-- :mode=xml: -->
        <rss version=\"2.0\" xmlns:media=\"http://search.yahoo.com/mrss/\">
        <channel>
        <title>Fuck Telus Tv</title>
        <link>http://fucktelustv.net</link>");
close LOG;

for ($i=0; $i<@flvs; $i++)
{
	@flvs[$1] =~ tr/A-Z/a-z/;
	#Create Title
	$title = dirname(@flvs[$i]);
	@array = split('/',$title);
	$title = @array[-1];
	#Create Description
	$description = basename(@flvs[$i]);
	$filename = $description;
	$description =~ s/.flv/ /;
	print @flvs[$i];
	$xml = (" 
        <link>http://fucktelustv.net</link>
	<item>
	<title>$description</title>
	<description>$description</description>
	<media:content url=\"$filename\"/>
	</item>\n");
	open LOG, ">>$xmlpath/list.xml" or die $!;
print LOG $xml;
}
print LOG "</channel>\n</rss>";
